/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import in.srain.cube.image.CubeImageView;
import in.srain.cube.image.ImageLoader;
import in.srain.cube.image.ImageLoaderFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.gmail.blackdog1987.carcare.App;
import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.entity.TCarSale;
import com.gmail.blackdog1987.carcare.util.ModelUtil;
import com.gmail.blackdog1987.carcare.util.StringFormater;
import com.gmail.blackdog1987.carcare.widget.ImgViewerWindow;

@EActivity(R.layout.activity_carsale_detail)
public class CarSaleDetailActivity extends BaseActivity implements
		OnClickListener {
	private Context mContext;
	private TCarSale mCar;

	// views
	@ViewById(R.id.carsale_detail_title)
	public TextView mTitle;
	@ViewById(R.id.carsale_detail_head)
	public CubeImageView mHead;
	@ViewById(R.id.carsale_detail_call)
	public View mCall;
	@ViewById(R.id.carsale_detail_location)
	public View mLocation;
	@ViewById(R.id.carsale_detail_contact)
	public View mContact;
	@ViewById(R.id.carsale_detail_info)
	public TextView mInfo;
	@ViewById(R.id.carsale_detail_price)
	public TextView mPrice;
	@ViewById(R.id.carsale_detail_img1)
	public CubeImageView mImg1;
	@ViewById(R.id.carsale_detail_img2)
	public CubeImageView mImg2;
	@ViewById(R.id.carsale_detail_img3)
	public CubeImageView mImg3;
	@ViewById(R.id.carsale_detail_img4)
	public CubeImageView mImg4;
	@ViewById(R.id.carsale_detail_img5)
	public CubeImageView mImg5;
	@ViewById(R.id.carsale_detail_color)
	public TextView mColor;
	@ViewById(R.id.carsale_detail_problem)
	public TextView mProblem;
	@ViewById(R.id.carsale_detail_capacity)
	public TextView mCapacity;
	@ViewById(R.id.carsale_detail_maintain)
	public TextView mMaintain;
//	@ViewById(R.id.carsale_detail_size)
//	public TextView mSize;
	@ViewById(R.id.carsale_detail_check)
	public TextView mCheck;
	@ViewById(R.id.carsale_detail_paizhao)
	public TextView mPaizhao;
	@ViewById(R.id.carsale_detail_jiaoqiangxian)
	public TextView mJiaoqiangxian;
	@ViewById(R.id.carsale_detail_date)
	public TextView mDate;
	@ViewById(R.id.carsale_detail_shangyexian)
	public TextView mShangyexian;
	@ViewById(R.id.carsale_detail_status)
	public TextView mStatus;
	@ViewById(R.id.carsale_detail_engine)
	public TextView mEngine;
	@ViewById(R.id.carsale_detail_drivetype)
	public TextView mDriveType;
	@ViewById(R.id.carsale_detail_transmission)
	public TextView mTransmission;
	@ViewById(R.id.carsale_detail_gastype)
	public TextView mGastype;
	@ViewById(R.id.carsale_detail_carlevel)
	public TextView mCarlevel;
	@ViewById(R.id.carsale_detail_tail)
	public TextView mPressure;
	@ViewById(R.id.carsale_detail_abs)
	public TextView mABS;
	@ViewById(R.id.carsale_detail_esp)
	public TextView mESP;
	@ViewById(R.id.carsale_detail_childseat)
	public TextView mChildSeat;
	@ViewById(R.id.carsale_detail_remotekey)
	public TextView mRemoteKey;
	@ViewById(R.id.carsale_detail_cruise)
	public TextView mCruise;
	@ViewById(R.id.carsale_detail_powersunroof)
	public TextView mPowerSunroof;
	@ViewById(R.id.carsale_detail_oxlight)
	public TextView mXenon;
	@ViewById(R.id.carsale_detail_cost)
	public TextView mCost;
	@ViewById(R.id.carsale_detail_phone)
	public TextView mPhone;
	@ViewById(R.id.carsale_detail_shop)
	public TextView mShop;
	
	private Intent imgViewIntent;

	@AfterViews
	public void afterViews() {
		mContext = this;
		initData();
		reg();
	}

	public void reg() {
		findViewById(R.id.back).setOnClickListener(this);
	}

	public void initData() {
		imgViewIntent = new Intent(CarSaleDetailActivity.this, ImgViewerWindow.class);
		mCar = (TCarSale) getIntent().getSerializableExtra("data");

		// set views data
		ImageLoader imageLoader = ImageLoaderFactory.create(mContext);
		if(mCar.headurl!=null&&mCar.headurl.length()!=0){
			App.loadImg(mHead, imageLoader, mCar.headurl);
		}
		if(mCar.img1!=null&&mCar.img1.length()!=0){
			App.loadImg(mImg1, imageLoader, mCar.img1);
			mImg1.setOnClickListener(this);
		}
		if(mCar.img2!=null&&mCar.img2.length()!=0){
			App.loadImg(mImg2, imageLoader, mCar.img2);
			mImg2.setOnClickListener(this);
		}
		if(mCar.img3!=null&&mCar.img3.length()!=0){
			App.loadImg(mImg3, imageLoader, mCar.img3);
			mImg3.setOnClickListener(this);
		}
		if(mCar.img4!=null&&mCar.img4.length()!=0){
			App.loadImg(mImg4, imageLoader, mCar.img4);
			mImg4.setOnClickListener(this);
		}
		if(mCar.img5!=null&&mCar.img5.length()!=0){
			App.loadImg(mImg5, imageLoader, mCar.img5);
			mImg5.setOnClickListener(this);
		}
		
		mTitle.setText(ModelUtil.getCarsaleTitle(mCar));
		mInfo.setText(ModelUtil.getCarsaleInfo(mCar));
		mPrice.setText(String.valueOf(mCar.getPrice()));
		mColor.setText(mCar.getColor());
		mCapacity.setText(mCar.getCapacity());
		mMaintain.setText(mCar.getMaintain());
//		mSize.setText(mCar.getSize());
		mCheck.setText(StringFormater.formatDay(new Date(mCar.getCheckTime())));
		mJiaoqiangxian.setText(mCar.getJiaoqiangxian());
		mProblem.setText(mCar.getProblemhistory());
		mShangyexian.setText(mCar.getShangyexian());
		mDate.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date(mCar.getFactorytime())));
		mStatus.setText(mCar.getFactorystatus());
		mEngine.setText(mCar.getEngine());
		mDriveType.setText(mCar.getDriveType());
		mTransmission.setText(mCar.getTransmission());
		mGastype.setText(mCar.getGasType());
		mCarlevel.setText(String.valueOf(mCar.getCarLevel()));
		mPhone.setText(mCar.getPhone());
		mShop.setText(mCar.getShop());
		// 亮点功能，需要修改背景
		if(mCar.getPressure()!=null&&1 == mCar.getPressure().intValue()){
			mPressure.setBackgroundResource(R.color.green);
		}
		if(mCar.getAbs()!=null&&1 == mCar.getAbs().intValue()){
			mABS.setBackgroundResource(R.color.green);
		}
		if(mCar.getEsp()!=null&&1 == mCar.getEsp().intValue()){
			mESP.setBackgroundResource(R.color.green);
		}
		if(mCar.getChildSeat()!=null&&1 == mCar.getChildSeat().intValue()){
			mChildSeat.setBackgroundResource(R.color.green);
		}
		if(mCar.getRemoteKey()!=null&&1 == mCar.getRemoteKey().intValue()){
			mRemoteKey.setBackgroundResource(R.color.green);
		}
		if(mCar.getCruise()!=null&&1 == mCar.getCruise().intValue()){
			mCruise.setBackgroundResource(R.color.green);
		}
		if(mCar.getPowerSunroof()!=null&&1 == mCar.getPowerSunroof().intValue()){
			mPowerSunroof.setBackgroundResource(R.color.green);
		}
		if(mCar.getXenon()!=null&&1 == mCar.getXenon().intValue()){
			mXenon.setBackgroundResource(R.color.green);
		}
		mCost.setText(String.valueOf(mCar.getPrice()));
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.carsale_detail_img1:
			imgViewIntent.putExtra("url", mCar.img1);
			startActivity(imgViewIntent);
			break;
		case R.id.carsale_detail_img2:
			imgViewIntent.putExtra("url", mCar.img2);
			startActivity(imgViewIntent);
			break;
		case R.id.carsale_detail_img3:
			imgViewIntent.putExtra("url", mCar.img3);
			startActivity(imgViewIntent);
			break;
		case R.id.carsale_detail_img4:
			imgViewIntent.putExtra("url", mCar.img4);
			startActivity(imgViewIntent);
			break;
		case R.id.carsale_detail_img5:
			imgViewIntent.putExtra("url", mCar.img5);
			startActivity(imgViewIntent);
			break;
		case R.id.back:
			finish();
			break;
		default:
			break;
		}

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(Activity.RESULT_OK == resultCode){
			finish();
		}
	}

	// click area
	@Click(R.id.back)
	void back() {
		finish();
	}

	@Click(R.id.carsale_detail_call)
	void call() {
		
	}

	@Click(R.id.carsale_detail_contact)
	void contact() {
		
	}

	@Click(R.id.carsale_detail_location)
	void location() {
		
	}

	@Click(R.id.carsale_detail_pay)
	void pay() {
		
		Intent i = new Intent(this, OrderCarSaleActivity_.class);
		i.putExtra("data", mCar);
		startActivityForResult(i, 1);
		
	}
}
